package net.confinanz.client.data;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;

import net.confinanz.client.controller.Controller;
import net.confinanz.client.exceptions.NotConnectedException;

public class Status implements Serializable {
	
	private static final long serialVersionUID = 5048629328667423631L;
	
	public static final String AKTIV 	= "1";
	public static final String INAKTIV 	= "2";
	public static final String EIGEN	= "3";
	public static final String FREMD	= "4";
	
	private static final String SELECT 	= "SELECT * FROM `meta_status` ";
	private static final String INSERT	= "INSERT INTO `meta_status` (`status_bezeichner`, `status_farbe`, `status_icon`) VALUES (";
	private static final String UPDATE	= "UPDATE `meta_status` SET ";

	private String id;
	private String bezeichner;
	private String farbe;
	private String icon;
	
	public Status() {
		this.setId("");
		this.setBezeichner("");
	}

	public Status(String id, Controller controller) throws SQLException, NotConnectedException {
		setId(id);
		fromDatabase(controller);
	}
	
	
//	##### Instanz Methoden #####

	private void setId(String id) {
		this.id = id;
	}
	
	public String getId() {
		return this.id;
	}
	
	public String getBezeichner() {
		return this.bezeichner;
	}
	
	public void setBezeichner(String bezeichner) {
		this.bezeichner = bezeichner;
	}

	public String getFarbe() {
		return farbe;
	}

	public void setFarbe(String farbe) {
		this.farbe = farbe;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String toStringExtended() {
		return (this.id + " : " + this.bezeichner + " : " + this.farbe + " : " + this.getIcon());
	}
	
	public String toStringDatabase() {
		return toStringExtended();
	}
	
	
//	##### Datenbank Methoden #####
	
	public void fromDatabase(Controller controller) throws SQLException, NotConnectedException {
		String sql = SELECT + "WHERE `status_id` = " + getId();
		controller.logMessage("SQL-QUERY: " + sql);
		ResultSet result = controller.getConnector().select(sql);
		
		if (result.first()) {
			setBezeichner(result.getString("status_bezeichner"));
			setFarbe(result.getString("status_farbe"));
			setIcon(result.getString("status_icon"));
			
			controller.logMessage("RESULT: " + toStringDatabase());
		} else throw new SQLException("There is no dataset with `status_id` = " + getId() + " in table `meta_status`.");
	}
	
	public void toDatabase(Controller controller) throws SQLException, NotConnectedException {
		String sql;
		if (getId().equals("")) {
			sql = INSERT + "'" + getBezeichner()
					+ "')";
			
			controller.logMessage("SQL-QUERY: " + sql);
			controller.getConnector().insert(sql);

			sql = Controller.INSERT_ID;
			controller.logMessage("SQL-QUERY: " + sql);
			ResultSet result = controller.getConnector().select(sql);
			
			if (result.first()) {
				setId(result.getString("id"));
			}
			controller.logMessage("RESULT: " + toStringDatabase());
		} else {
			sql = UPDATE + "`status_bezeichner` = '" + getBezeichner()
					+ "', `status_farbe` =  '" + getFarbe()
					+ "', `status_icon` = '" + getIcon()
					+ "' WHERE `status_id` = " + getId();
			
			controller.logMessage("SQL-QUERY: " + sql);
			controller.getConnector().insert(sql);
			controller.logMessage("RESULT: " + toStringDatabase());
		}
	}
	
	
//	##### Override Methoden #####
	
	@Override
	public String toString() {
		return this.bezeichner;
	}
	
	@Override
	public boolean equals(Object object) {
		return ((object instanceof Status) && (((Status) object)).getId().equals(this.getId()));
	}
}